package com.ruoyi.web.controller.proconsupagr;

import java.io.IOException;
import java.util.List;

import com.ruoyi.common.config.Global;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.domain.BeCompleted;
import com.ruoyi.domain.PlanToLog;
import com.ruoyi.domain.ProjectContract;
import com.ruoyi.framework.util.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.domain.ProConSupAgr;
import com.ruoyi.service.IProConSupAgrService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 工程合同及补充协议Controller
 *
 * @author ruoyi
 * @date 2020-06-18
 */
@Controller
@RequestMapping("/ruoyi/agr")
public class ProConSupAgrController extends BaseController {
    private String prefix = "ruoyi/agr";

    @Autowired
    private IProConSupAgrService proConSupAgrService;

    @RequiresPermissions("ruoyi:agr:view")
    @GetMapping()
    public String agr() {
        return prefix + "/agr";
    }

    /**
     * 首页的详情页面向工程合同页面传递一个id
     * @param bId
     * @param map
     * @return
     */
    @RequiresPermissions("ruoyi:agr:view")
    @GetMapping("/agrs/{bId}")
    public String completeds(@PathVariable("bId") Long bId, ModelMap map) {
        ProConSupAgr proConSupAgr = proConSupAgrService.selectProConSupAgrById(bId);
        map.put("proConSupAgr", proConSupAgr);
        return prefix + "/agrs";
    }

    /**
     * 查询工程合同及补充协议列表
     */
    @RequiresPermissions("ruoyi:agr:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ProConSupAgr proConSupAgr) {
        startPage();
        List<ProConSupAgr> list = proConSupAgrService.selectProConSupAgrList(proConSupAgr);
        return getDataTable(list);
    }

    /**
     * 查询对应项目下的对应工程合同
     */
    @RequiresPermissions("ruoyi:contract:lists")
    @PostMapping("/lists")
    @ResponseBody
    public TableDataInfo lists(Long sId) {
        startPage();
        List<ProConSupAgr> list = proConSupAgrService.selectProConSupAgrLists(sId);

        return getDataTable(list);
    }

    /**
     * 首页下对应的工程合同
     */
    @RequiresPermissions("ruoyi:log:listss")
    @PostMapping("/listss")
    @ResponseBody
    public TableDataInfo listss(Long idss) {
        startPage();
        List<ProConSupAgr> list = proConSupAgrService.selectProConSupAgrLists(idss);
        return getDataTable(list);
    }

    /**
     * 导出工程合同及补充协议列表
     */
    @RequiresPermissions("ruoyi:agr:export")
    @Log(title = "工程合同及补充协议", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ProConSupAgr proConSupAgr) {
        List<ProConSupAgr> list = proConSupAgrService.selectUserexport(proConSupAgr);
        ExcelUtil<ProConSupAgr> util = new ExcelUtil<ProConSupAgr>(ProConSupAgr.class);
        return util.exportExcel(list, "工程合同及补充协议");
    }

    /**
     * 新增工程合同及补充协议
     */
    @GetMapping("/add/{pId}")
    public String add(@PathVariable("pId") Long pId,ModelMap map) {
        map.put("pId",pId);
        return prefix + "/add";
    }

    /**
     * 新增保存工程合同及补充协议
     */
    @RequiresPermissions("ruoyi:agr:add")
    @Log(title = "工程合同及补充协议", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ProConSupAgr proConSupAgr, @RequestParam("file") MultipartFile file) throws IOException {
        //获取登录名 并且自动添加上传人
        String name = ShiroUtils.getLoginName();
        proConSupAgr.setpTheHeir(name);
        //获取文件上传路径
        String filePath = Global.getUploadPath();
        // 上传并返回新文件名称
        String fileName = FileUploadUtils.upload(filePath, file);
        proConSupAgr.setpUploading(fileName);
        return toAjax(proConSupAgrService.insertProConSupAgr(proConSupAgr));
    }

    /**
     * 修改工程合同及补充协议
     */
    @GetMapping("/edit/{pId}")
    public String edit(@PathVariable("pId") Long pId, ModelMap mmap) {
        ProConSupAgr proConSupAgr = proConSupAgrService.selectProConSupAgrById(pId);
        mmap.put("proConSupAgr", proConSupAgr);
        return prefix + "/edit";
    }

    /**
     * 修改保存工程合同及补充协议
     */
    @RequiresPermissions("ruoyi:agr:edit")
    @Log(title = "工程合同及补充协议", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ProConSupAgr proConSupAgr) {
        return toAjax(proConSupAgrService.updateProConSupAgr(proConSupAgr));
    }

    /**
     * 删除工程合同及补充协议
     */
    @RequiresPermissions("ruoyi:agr:remove")
    @Log(title = "工程合同及补充协议", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(proConSupAgrService.deleteProConSupAgrByIds(ids));
    }

    /**
     * 下载功能
     */
    @GetMapping("downloadFile")
    public void resourceDownload(@PathVariable("pUploading") String fAccessory, HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        // 本地资源路径
        String localPath = Global.getProfile();
        // 数据库资源地址
        String downloadPath = localPath + StringUtils.substringAfter(fAccessory, Constants.RESOURCE_PREFIX);
        // 下载名称
        String downloadName = StringUtils.substringAfterLast(downloadPath, "/");
        response.setCharacterEncoding("utf-8");
        response.setContentType("multipart/form-data");
        response.setHeader("Content-Disposition",
                "attachment;fileName=" + FileUtils.setFileDownloadHeader(request, downloadName));
        FileUtils.writeBytes(downloadPath, response.getOutputStream());
    }
    /**
     * 查询工程合同及补充协议列表
     */
    @RequiresPermissions("ruoyi:agr:list")
    @PostMapping("/lists/{pId}")
    @ResponseBody
    public TableDataInfo lists(@PathVariable ("pId") String pId) {
        startPage();
        List<ProConSupAgr> list = proConSupAgrService.selectpId(pId);
        return getDataTable(list);
    }

}
